package com.xtrip.common.tag;

import java.io.IOException;
import java.util.List;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.BodyTagSupport;

import org.apache.oro.text.regex.MalformedPatternException;
import org.apache.oro.text.regex.MatchResult;
import org.apache.oro.text.regex.Pattern;
import org.apache.oro.text.regex.PatternCompiler;
import org.apache.oro.text.regex.PatternMatcher;
import org.apache.oro.text.regex.PatternMatcherInput;
import org.apache.oro.text.regex.Perl5Compiler;
import org.apache.oro.text.regex.Perl5Matcher;
import org.apache.taglibs.standard.lang.support.ExpressionEvaluatorManager;

import com.tcl.platform.validate.Result;

public class CitySelectTag extends BodyTagSupport {
	private static final long serialVersionUID = -106119360064591146L;
	private String id;
	private String codeName;
	private String bankName;
	private String settleName;
	private String value;
	private String display;
	
	public int doStartTag()throws JspException{
		Result res = (Result) pageContext.getRequest().getAttribute("com.hyt.cod.validate.Result");
//		if (res == null || res.isValid()) {
//			return SKIP_BODY;
//		}
		StringBuffer code = new StringBuffer();
		JspWriter writer = pageContext.getOut();
		try {
			writer.println(code.toString());
		} catch (IOException e) {
			e.printStackTrace();
			throw new JspException(e);
		}
		return super.doStartTag();
	}

	public String getFinalOnclick(String onclick) {
		PatternMatcher matcher;
		PatternCompiler compiler;
		Pattern pattern = null;
		PatternMatcherInput input;
		MatchResult result;

		compiler = new Perl5Compiler();
		matcher = new Perl5Matcher();
		try {
			pattern = compiler.compile("\\$\\{[^\\$\\{\\}]*\\}");
		} catch (MalformedPatternException e) {
			System.err.println("Bad pattern.");
			System.err.println(e.getMessage());
			return null;
		}
		input = new PatternMatcherInput(onclick);
		while (matcher.contains(input, pattern)) {
			// Since we're still in the loop, fetch match that was found.
			result = matcher.getMatch();

			System.out.println("Begin offset: " + result.beginOffset(0));
			System.out.println("End offset: " + result.endOffset(0));
			
			String value = null;
			try {
				value = evaluateExpressions(result.toString());
			} catch (JspException e) {
				e.printStackTrace();
			}
			onclick = onclick.substring(0, result.beginOffset(0)) + value
					+ onclick.substring(result.endOffset(0));
			input = new PatternMatcherInput(onclick);
		}
		System.out.println(onclick);
		return onclick;
	}
	private String evaluateExpressions(String elStr) throws JspException {
		String value = null;
		if (elStr!=null){
			Object r = ExpressionEvaluatorManager.evaluate("", elStr, Object.class, this, pageContext);
			
			if (r==null)value = "";
			else value = r.toString();
		}
		return value;
	}
	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getCodeName() {
		return codeName;
	}

	public void setCodeName(String codeName) {
		this.codeName = codeName;
	}

	public String getBankName() {
		return bankName;
	}

	public void setBankName(String bankName) {
		this.bankName = bankName;
	}

	public String getSettleName() {
		return settleName;
	}

	public void setSettleName(String settleName) {
		this.settleName = settleName;
	}

	public static long getSerialversionuid() {
		return serialVersionUID;
	}

	public String getValue() {
		return value;
	}

	public void setValue(String value) {
		this.value = value;
	}

	public String getDisplay() {
		return display;
	}

	public void setDisplay(String display) {
		this.display = display;
	}
}
